<?php
App::import('Vendor', 'php-excel-reader/excel_reader2');
class PointFieldsController extends AppController{ 
	var $name = 'PointFields';
	var $use = array('PointField');
	var $components = array('RequestHandler', 'Filter', 'FileUpload');
	var $helpers = array('Js' => array('Jquery'), 'xls');
	var $paginate = array(
        'limit' => 5,
        'order' => array(
            'PointField.id' => 'asc'
        )
    );
    function index(){
    	$filter = $this->Filter->process($this);
    	//$this->set('list_year', );
    	$this->set('pfs', $this->paginate('PointField', $filter));
    	$this->set('isFilter', $filter);
    }
	function view($id = null) {
	$this->PointField->id = $id;
	$this->set('pt_info', $this->PointField->read());
	}
	function add() {
		$this->PointField->create();
		$year = array();
		$maxdate = getdate();
		for($i = 2007;$i<=$maxdate["year"];$i++)
			$year[$i]=$i;
		$this->set('list_years',$year);
		if (!empty($this->data))
		{
			if ($this->PointField->save($this->data)) {
				$this->flash(__('Thêm thành công.', true), array('action' => 'index'));
			}
			else{
				$this->flash(__('Thao tác thất bại. Vui lòng thử lại', true), array('action' => 'index'));
			}
		}
	}
function edit($id = null) {
	$this->PointField->id = $id;
	$year = array();
	$maxdate = getdate();
	for($i = 2007;$i<=$maxdate["year"];$i++)
		$year[$i]=$i;
	$this->set('list_years',$year);
	$this->set('id', $id);
	if (empty($this->data)) {
		$this->data = $this->PointField->read();
	} else {
		if ($this->PointField->save($this->data)) {
			$this->Flash(__('Cập nhật thành công thông tin thang điểm.',true),array('action' => 'index'));
		}
		else{
			$this->flash(__('Thao tác thất bại. Vui lòng thử lại', true), array('action' => 'index'));
		}
	}
}
	function del($id) {
		$this->PointField->delete($id);
		$this->redirect(array('action' => 'index'));
	}
	function delMulti(){
		if($this->data['SelectedPfs'])
		{
			$listSelect = array();
			foreach($this->data['SelectedPfs'] as $key => $value) {
				if($value != '0') {
					array_push($listSelect, $value);
					
					//$this->UnionMember->delete($value);
					//$this->flash(__($value, true), array('action' => 'index'));
		 		}
			}
			$this->set('pfs', $this->PointField->find('all', array('conditions'=>array('PointField.id'=>$listSelect))));
			$this->Session->write('listDel', $listSelect);
		}
		else{
			if($this->Session->check('listDel')){
				foreach($this->Session->read('listDel') as $value){
					$this->PointField->delete($value);
					
				}
				$this->Session->delete('listDel');
				$this->flash(__('Đã xóa thành công', true), array('action' => 'index'));
			}
			else{
				$this->flash(__('Thao tác thất bại, vui lòng thử lại', true), array('action' => 'index'));
			}
		}
	}
	function exportXls(){
		$filter = $this->Filter->process($this);
		$this->set('pfs', $this->PointField->find('all', array('conditions'=>$filter)));
		}
	}	

	function importXls(){
		//process upload file
        $this->FileUpload->uploadDir = 'files';
		$this->FileUpload->fileModel = null;
        $this->FileUpload->allowedTypes=array('xml','xls');
        $this->FileUpload->maxFileSize='2M';

        if($this->FileUpload->uploadFile('listmems')) {
            //process import file into database
            $data = new Spreadsheet_Excel_Reader("files\\".$this->FileUpload->finalFile,true,'UTF-8');
            $this->set('data', $data);
            $tempdata = array('UnionMember'=>array('id'=>'','firstname'=>'', 'lastname'=>''));
            for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {

                if(empty($data->sheets[0]['cells'][$i][1])) {
                	$this->FileUpload->removeFile($this->FileUpload->finalFile);
                    $this->flash(__('Thao tác thất bại. Vui lòng thử lại', true), array('action' => 'index'));
                }else {
                    $tempdata['UnionMember']['id'] = $data->sheets[0]['cells'][$i][1];
                };
                
                if(empty($data->sheets[0]['cells'][$i][2])) {
                	$this->FileUpload->removeFile($this->FileUpload->finalFile);
                    $this->flash(__('Thao tác thất bại. Vui lòng thử lại', true), array('action' => 'index'));
                }else {
                    $tempdata['UnionMember']['firstname'] = $data->sheets[0]['cells'][$i][2];
                };
				
                if(empty($data->sheets[0]['cells'][$i][3])) {
                	$this->FileUpload->removeFile($this->FileUpload->finalFile);
                    $this->flash(__('Thao tác thất bại. Vui lòng thử lại', true), array('action' => 'index'));
                }else {
                    $tempdata['UnionMember']['lastname'] = $data->sheets[0]['cells'][$i][3];
                };
                //Prepare data to validate
                $this->UnionMember->set($tempdata);
                if($this->UnionMember->validates()) {
                    //Checking id if it existed
                    if($this->UnionMember->find('count',array('conditions' => array('UnionMember.id' => $tempdata['UnionMember']['id'])))) {
                        $this->FileUpload->removeFile($this->FileUpload->finalFile);
                    	$this->flash(__('Thao tác thất bại do trùng mã số. Vui lòng thử lại', true), array('action' => 'index'));
                    }
                    else { //validate valid
                        if ($this->UnionMember->save()) {
							//$this->flash(__('Thao tác thành công 1', true), array('action' => 'index'));
					}
					else
						$this->flash(__('Thao tác thất bại. Vui lòng thử lại', true), array('action' => 'index'));
					}
				}
                else    //validate is not valid
                {
                    
                    $this->FileUpload->removeFile($this->FileUpload->finalFile);
                    $this->flash(__('Thêm mới bị lỗi từ SV có mã '.$data->sheets[0]['cells'][$i][1].", do lỗi dữ liệu nhập", true), array('action'=>'index'));
                }
            }
            $this->FileUpload->removeFile($this->FileUpload->finalFile);
            $this->flash(__('Thêm mới từ file thành công', true), array('action' => 'index'));
            
        }
        else {
            $this->flash($this->FileUpload->showErrors());
        }
	}
?>